from rest_framework import serializers, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import HTTP_504_GATEWAY_TIMEOUT
from rest_framework_jwt.authentication import jwt_decode_handler
from six_it.models import Question
from six_it.serializers.question import QuestionSerializers
from rest_framework.views import APIView
from rest_framework.generics import CreateAPIView,GenericAPIView
from rest_framework.mixins import RetrieveModelMixin,CreateModelMixin,DestroyModelMixin
from six_it.serializers.lable import LabelRRetrieveSerializers
from six_it.models import Label,Question,Article,User,Reply
class LabelView(RetrieveModelMixin,GenericAPIView):
    queryset = Label
    serializer_class = LabelRRetrieveSerializers
    lookup_field = 'id'
    def get(self,request,id):
        return self.retrieve(request)

class LabelfocusinAPIView(APIView):
    def put(self, request, id):
        try:
            token = self.request.auth
            toke_user = jwt_decode_handler(token)
            user_id = toke_user["user_id"]
        except Exception as e:
            return Response('请先登录', status=404)
        try:
            label = Label.objects.get(id=id)
        except Exception as e:
            return Response(status=HTTP_504_GATEWAY_TIMEOUT)
        label.users.add(user_id)
        return Response({'message': '谢谢支持', "success": True})
class LabelfocusoutAPIView(APIView):
    def put(self,request,id):
        try:
            token = self.request.auth
            toke_user = jwt_decode_handler(token)
            user_id = toke_user["user_id"]
        except Exception as e:
            return Response('请先登录', status=404)
        try:
            label = Label.objects.get(id=id)
        except Exception as e:
            return Response(status=HTTP_504_GATEWAY_TIMEOUT)
        label.users.remove(user_id)

        return Response({'message': '谢谢支持', "success": True})